package com.bank.service;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.bank.dao.CustomerDao;
import com.bank.entity.Customer_info;
import com.bank.util.ImportExeclUtil;

import java.io.InputStream;
import java.util.List;

@Service
public class CustomerService {
	
	Logger logger = Logger.getLogger(CustomerService.class);
	
	
	@Autowired
	CustomerDao customerDao;
	/***
	 * 
	 * 保存用户
	 * 
	 * 
	 * */
	public void addCustomer(Customer_info customer){
		logger.debug("保存客户");
		customerDao.save(customer);
	}
	
	/**
	 * 查询用户
	 * 
	 * */
	public Customer_info queryCustomer(String customer_number ){
		return customerDao.getCustomer(customer_number);
	}
	
	public void updateCustomer(Customer_info customer){
		customerDao.updateCustomer(customer);
	}
	
	
	/**
	 * 批量导入客户信息
	 * @throws Exception 
	 * */
	 public int batchImport( MultipartFile file_name){
		 try {
			 	int sum = 0;
		 		List<List<Object>> listob = null;  
		 		InputStream in=file_name.getInputStream();
		 		listob = new ImportExeclUtil().getWorkListByExecl(in, file_name);
		 		in.close();
		 		logger.debug("size="+listob.size());
		 		//该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
		        for (int i = 0; i < listob.size(); i++) {
		        	sum++;
		            List<Object> lo = listob.get(i);
		            logger.debug("lo.size:"+lo.size());
		            Customer_info customer = new Customer_info();
		            customer.setCustomer_number(String.valueOf(lo.get(0)));
		            customer.setCustomer_name(String.valueOf(lo.get(1)));
		            customer.setCustomer_id_type(String.valueOf(lo.get(2)));
		            customer.setCustomer_idno(String.valueOf(lo.get(3)));
		            customer.setCustomer_phone(String.valueOf(lo.get(4)));
		            customer.setCustomer_email(String.valueOf(lo.get(5)));
		            customer.setCustomer_address(String.valueOf(lo.get(6)));
		            customerDao.save(customer);
		        }
		        return sum;

		} catch (Exception e) {
			return -1;
		}
	}
}
